﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.system.code
{
    public partial class edit : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["code_id"] != null) ? Convert.ToInt32(Request.QueryString["code_id"].ToString()) : 0;
                this.ViewState["CodeId"] = num;
                string str = string.Empty;
                string commandText = "SELECT * FROM [dbo].[Sys_Code] WHERE [CodeId]=@CodeId";
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CodeId", SqlDbType.Int) };
                commandParameters[0].Value = num;
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                if (table.Rows.Count > 0)
                {
                    str = table.Rows[0]["CodeFlag"].ToString();
                    this.ViewState["CodeFlag"] = str;
                    this.txtCodeNo.Text = table.Rows[0]["CodeNo"].ToString();
                    this.txtCodeName.Text = table.Rows[0]["CodeName"].ToString();
                    commandText = "SELECT * FROM [dbo].[Sys_Code] WHERE [CodeNo]=@CodeNo AND [CodeFlag] IS NULL";
                    commandParameters = new SqlParameter[] { new SqlParameter("@CodeNo", SqlDbType.NVarChar, 200) };
                    commandParameters[0].Value = str;
                    DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText, commandParameters).Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.litCodeName.Text = table2.Rows[0]["CodeName"].ToString();
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                string str3 = "SELECT * FROM [dbo].[Sys_Code] a,[dbo].[Sys_Code] b WHERE a.[CodeId]=@CodeId AND a.[CodeFlag]=b.[CodeFlag] AND a.[CodeId]<>b.[CodeId] AND b.[CodeNo] = @CodeNo";
                SqlParameter[] parameterArray2 = new SqlParameter[2];
                parameterArray2[0] = new SqlParameter("@CodeId", SqlDbType.Int);
                parameterArray2[0].Value = this.ViewState["CodeId"].ToString();
                parameterArray2[1] = new SqlParameter("@CodeNo", SqlDbType.NVarChar, 200);
                parameterArray2[1].Value = this.txtCodeNo.Text;
                DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3, parameterArray2).Tables[0];
                if (table3.Rows.Count > 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("编号已经存在！", "edit.aspx?code_id=" + this.ViewState["CodeId"].ToString());
                }
                else
                {
                    str3 = "UPDATE [dbo].[Sys_Code] SET [CodeNo]=@CodeNo,[CodeName]=@CodeName WHERE [CodeId]=@CodeId";
                    parameterArray2 = new SqlParameter[3];
                    parameterArray2[0] = new SqlParameter("@CodeId", SqlDbType.Int);
                    parameterArray2[0].Value = this.ViewState["CodeId"].ToString();
                    parameterArray2[1] = new SqlParameter("@CodeNo", SqlDbType.NVarChar, 200);
                    parameterArray2[1].Value = this.txtCodeNo.Text;
                    parameterArray2[2] = new SqlParameter("@CodeName", SqlDbType.NVarChar, 200);
                    parameterArray2[2].Value = this.txtCodeName.Text;
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, str3, parameterArray2);
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("代码修改成功！", "func.aspx?code_no=" + this.ViewState["CodeFlag"].ToString());
                }
            }
        }
 

    }
}
